﻿using System;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace LoQui.SharedClasses
{
    //test here for commit
    //comment again
    public class DatabaseHandler
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        SqlDataReader dr = null;
        DataTable dt = null;

        private bool connectToDatabase()
        {
            try
            {
                //Data Source=(LocalDB)\v11.0;AttachDbFilename="E:\Dinos Mapp\1.Utbildning\Dropbox\Datasystemutvecklingsprogrammet 180 Hp\Default Workspaces\C#.NET Default Workspace\Labb3_versionMax\Labb3_versionMax\App_Data\Database.mdf";Integrated Security=True
                string connectionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=E:\\Dinos Mapp\\1.Utbildning\\Dropbox\\Datasystemutvecklingsprogrammet 180 Hp\\Default Workspaces\\C#.NET Default Workspace\\Labb3_versionMax\\Labb3_versionMax\\App_Data\\Database.mdf;Integrated Security=True";
                //string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                conn = new SqlConnection(connectionString);
                dt = new DataTable();
                conn.Open();
                return true;
            }
            catch (Exception ex)
            {
                Console.Write("ATTENTION!!!  Connection to the Database has FAILD!!!");
                Console.Write("Exception == " + ex);
            }
            return false;
        }


        private void closeConnection()
        {
            try
            {
                conn.Close();
            }
            catch (Exception ex)
            {
                Console.Write("Exception == " + ex);
            }
        }

        public DataTable selectData(string inQuery)
        {

            try
            {
                if (connectToDatabase() == true)
                {
                    cmd = new SqlCommand(inQuery, conn);
                    dr = cmd.ExecuteReader();
                    dt.Load(dr);
                }
            }
            catch (Exception ex)
            {
                Console.Write("Exception == " + ex);

            }
            finally
            {
                closeConnection();
            }
            return dt;
        }

        public void insertIntoDatabase(string inQuery)
        {
            try
            {
                if (connectToDatabase() == true)
                {
                    cmd = new SqlCommand(inQuery, conn);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Console.Write("Exception == " + ex);
            }
            finally
            {
                closeConnection();
            }
        }

        public void upateTableFromDatabase(string inQuery)
        {
            try
            {
                if (connectToDatabase() == true)
                {
                    cmd = new SqlCommand(inQuery, conn);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Console.Write("Exception == " + ex);
            }
            finally
            {
                closeConnection();
            }

        }
        public void deleteDataFromTable(string inQuery)
        {
            try
            {
                if (connectToDatabase() == true)
                {
                    cmd = new SqlCommand(inQuery, conn);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Console.Write("Exception == " + ex);
            }
            finally
            {
                closeConnection();
            }

        }
    }
}